const logger = require('./logger')

module.exports = (opts) => {
  const loggerMiddleware = logger(opts);
  return (ctx, next) => {
    return loggerMiddleware(ctx, next).catch(e => {
      if (ctx.status < 500) {
        ctx.status = 500;
      }
      ctx.log.error(e.stack);
      ctx.status.logged = true;
      ctx.throw(e);
    })
  }
}